﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SZSLSystem.Common;

namespace SZSLSystem.Web.Admin.Role
{
    public partial class Edit : SZSLSystem.Web.UI.ManagePage
    {
        private List<CheckBoxList> CK_List = new List<CheckBoxList>();

        #region 全局变量


        #endregion 

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            if (!Page.IsPostBack)
            {
                
                ShowInfo(Guid.Parse(Request.QueryString["ID"]));

                BindData();
            }
        }

       /// <summary>
        /// 赋值操作
       /// </summary>
       /// <param name="ID">物流编号</param>
        private void ShowInfo(Guid ID)
        {
            //
            //角色
            //
            SZSLSystem.BLL.Role bll = new SZSLSystem.BLL.Role();
            SZSLSystem.Model.Role model = bll.GetModel(ID);

            this.txtName.Text = model.RoleName;
            this.txtDescription.Text = model.Description;

            SZSLSystem.BLL.RoleAuthority aubll = new BLL.RoleAuthority();
            DataTable tb = aubll.GetList("RoleID='" + ID + "'").Tables[0];

            for (int i = 0; i < tb.Rows.Count; i++)
            {
                for (int j = 0; j < this.chkAuthority.Items.Count; j++)
                {
                    if (tb.Rows[i]["AuthorityID"].ToString() == this.chkAuthority.Items[j].Value.ToString())
                    {
                        this.chkAuthority.Items[j].Selected = true;
                    }
                }
                //this.chkAuthority.Items.FindByValue(tb.Rows[i]["AuthorityID"].ToString()).Selected = true;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                //
                //角色
                //
                SZSLSystem.BLL.Role bll = new SZSLSystem.BLL.Role();
                SZSLSystem.Model.Role model = bll.GetModel(Guid.Parse(Request.QueryString["ID"]));
             
                model.RoleName = this.txtName.Text.Trim();
                model.Description = this.txtDescription.Text.Trim();
                bll.Update(model);

                SZSLSystem.BLL.RoleAuthority roleaubll = new BLL.RoleAuthority();
                roleaubll.DeletebyRoleID(model.ID);

                /*
                for (int i = 0; i < this.chkAuthority.Items.Count; i++)
                {
                    if (this.chkAuthority.Items[i].Selected)
                    {
                        GQTSystem.Model.RoleAuthority aumodel = new Model.RoleAuthority();
                        aumodel.ID = Guid.NewGuid();
                        aumodel.RoleID = model.ID;
                        aumodel.AuthorityID = Guid.Parse(this.chkAuthority.Items[i].Value.ToString());
                        aumodel.Sort = i + 1;
                        roleaubll.Add(aumodel);
                    }
                }
                */
                
                foreach (RepeaterItem ri in Repeater1.Items)
                {
                    CheckBoxList ckl = ri.FindControl("chk_Authority") as CheckBoxList;
                    for (int i = 0; i < ckl.Items.Count; i++)
                    {
                        if (ckl.Items[i].Selected)
                        {
                            SZSLSystem.Model.RoleAuthority aumodel = new Model.RoleAuthority();
                            aumodel.ID = Guid.NewGuid();
                            aumodel.RoleID = model.ID;
                            aumodel.AuthorityID = Guid.Parse(ckl.Items[i].Value.ToString());
                            aumodel.Sort = i + 1;
                            roleaubll.Add(aumodel);
                        }
                    }
                }

                //
                //保存日志
                //
                SaveLogs("修改系统角色成功！", 0);

                JscriptPrint("修改系统角色成功啦！", "List.aspx", "Success");
            }
            catch (Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>修改系统角色失败啦！", "", "Error");
            }
        }









        //---------------------------------------------hc

        protected void BindData()
        {
            SZSLSystem.BLL.Authority bll = new BLL.Authority();
            DataTable tb = bll.GetList("Label=1 order by Sort asc").Tables[0];
            this.Repeater1.DataSource = tb;
            this.Repeater1.DataBind();
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                CheckBoxList drp = e.Item.FindControl("chk_Authority") as CheckBoxList;
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                SZSLSystem.BLL.Authority bll = new BLL.Authority();
                DataTable tb0 = bll.GetList("Label=0 and ParentID='" + rowv["ID"].ToString() + "' order by Sort asc").Tables[0];
                drp.DataSource = tb0;
                drp.DataValueField = "ID";
                drp.DataTextField = "AuthorityName";
                drp.DataBind();

                SZSLSystem.BLL.RoleAuthority aubll = new BLL.RoleAuthority();
                DataTable tb = aubll.GetList("RoleID='" + Guid.Parse(Request.QueryString["ID"]) + "'").Tables[0];

                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    for (int j = 0; j < drp.Items.Count; j++)
                    {
                        if (tb.Rows[i]["AuthorityID"].ToString() == drp.Items[j].Value.ToString())
                        {
                            drp.Items[j].Selected = true;
                        }
                    }
                }

                CK_List.Add(drp);
            }
        }
    }
}
